package com.zycfc.zsf.boot.serializer.hessian;

import com.caucho.hessian.io.*;
import java.lang.ref.*;
import java.io.*;

public class SoftReferenceSerializer extends AbstractSerializer implements ObjectSerializer
{
    public Serializer getObjectSerializer() {
        return (Serializer)this;
    }
    
    public void writeObject(final Object obj, final AbstractHessianOutput out) throws IOException {
        if (out.addRef(obj)) {
            return;
        }
        final SoftReference<Object> data = (SoftReference<Object>)obj;
        final int refV = out.writeObjectBegin(SoftReference.class.getName());
        if (refV == -1) {
            out.writeInt(1);
            out.writeString("ref");
            out.writeObjectBegin(SoftReference.class.getName());
        }
        if (data != null) {
            final Object ref = data.get();
            if (null != ref) {
                out.writeObject(ref);
            }
            else {
                out.writeNull();
            }
        }
        else {
            out.writeNull();
        }
    }
}
